var app = getApp();
var time = require('../../../utils/util.js');
Page({
  /** 页面的初始数据 **/
  data: {
    selectRecommendURL: app.globalData.bookUrl + '/Recommend/selectRecommend.do',
    currentPage: 1,
    recommendList:[],
    // RecommendList: [
    //   {
    //     name: '孟祥玥',
    //     word: '墨菲定律，不错的一本书，值得观看，强力推荐 。墨菲定律，不错的一本书，值得观看，强力推荐 。墨菲定律，不错的一本书，值得观看,强力推荐 。墨菲定律，不错的一本书，值得观看，值得观看，值得观看,强力推荐 。',
    //     num: '0',
    //     people: '22', collected: 1
    //   },
    //   {
    //     name: '李光亮',
    //     word: '墨菲定律，不错的一本书，值得观看，强力推荐 。墨菲定律，不错的一本书，值得观看，强力推荐 。墨菲定律，不错的一本书，值得观看,强力推荐 。墨菲定律，不错的一本书，值得观看，值得观看，值得观看,强力推荐 。',
    //     num: '0',
    //     people: '22', collected: 1
    //   }
    // ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this ;
    that.setData({
      bookName: options.bookName,
      bookAuthor: options.bookAuthor
    });
    that.getRecommendDetail(that.data.currentPage);
  },
  getRecommendDetail: function (currentPage) {

    var that = this;
    wx.request({
      url: that.data.selectRecommendURL,
      data: {
        bookName: that.data.bookName,
        bookAuthor: that.data.bookAuthor,
        currentPage: currentPage,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.showRecommendDetail(res.data.data); //向前台传递数据
        if (res.data.data != null) {
          that.setData({
            currentPage: currentPage + 1,
            totalElements: res.data.data.totalElements
          });
        }
      },
      fail: function (error) {
        // fail
        console.log(error)
      }
    })
  },
  showRecommendDetail: function (recommend) {
    var that = this;
    var readersNameAll=[];
    var recommends = that.data.recommendList;
    var content0 = recommend.content[0];
    var recommendImg = app.globalData.bookUrl + content0.recommendImg + '?' + Math.random() / 9999;
    var createTime = time.formatTime(content0.createTime, 'Y-M-D h:m:s');
    if (recommend != null) {
      for (var idx in recommend.content) {
        var content = recommend.content[idx];
        var readersName = content.readersName;
        var recommendReason = content.recommendReason;
        var headImg = content.headImg;
        readersNameAll.push(readersName);
        var temp = {
          readersName: readersName,
          recommendReason: recommendReason,
          headImg: headImg
        }
        recommends.push(temp);
      }
      that.setData({
        readersNameAll: readersNameAll,
        recommendImg: recommendImg,
        createTime: createTime,
        recommendList: recommends
      });
      wx.hideLoading({
        title: '玩命加载中',
      })
    } else {
      wx.showToast({
        title: "没有新推荐",
        image: '../../images/icon/fail.png',
        duration: 1500
      })
    }
  },
  // 更改点赞状态
  onCollectionTap: function (event) {
    // 获取当前点击下标
    var index = event.currentTarget.dataset.index;
    console.log(index)
    // data中获取列表
    var message = this.data.RecommendList;
    for (let i in message) { //遍历列表数据
      if (i == index) { //根据下标找到目标
        var collectStatus = false
        if (message[i].collected == 0) { //如果是没点赞+1
          // collectStatus = true
          message[i].collected = parseInt(message[i].collected) + 1
          message[i].people = parseInt(message[i].people) + 1
        } else {
          // collectStatus = false
          message[i].collected = parseInt(message[i].collected) - 1
          message[i].people = parseInt(message[i].people) - 1
        }
        wx.showToast({
          title: collectStatus ? '收藏成功' : '取消收藏',
        })
      }
    }
    this.setData({
      RecommendList: message
    })
  },
  clickNum: function (event) {
    // 获取当前点击下标
    var index = event.currentTarget.dataset.index;
    console.log(index)
    var thisnum = event.target.dataset.num
    var message = this.data.RecommendList;

    for (let i in message) { //遍历列表数据
      if (i == index) { //根据下标找到目标
        console.log(message)
        if (message[i].num == 1) {
          message[i].num = 0
        }
        else {
          console.log(message[i].num)
          message[i].num = 1
        }
      }
    }
    this.setData({
      RecommendList: message
    })

  },
  //显示/隐藏  全部文章
  // clickNum: function (e) {
  //   if (thisnum == 1) {
  //     this.setData({
  //       _num: 0
  //     })
  //   } else {
  //     this.setData({
  //       _num: 1
  //     })
  //   }
  // }, 

})